import { Injectable } from '@angular/core';
import { v4 as guid } from "uuid";
import { ScenarioConfig, PiplineConifg, SiteNodeConfig, SrmConfig } from '../models';
@Injectable()
export class ScenarioUtilService {

  private scenario: ScenarioConfig = {
    Lines: [],
    Srms: []
  };
  constructor() { }

  loadScenario(scenario: ScenarioConfig) {
    this.scenario = scenario;
  }

  addPipline(item: PiplineConifg) {
    const Id = item.Id ?? guid();
    this.scenario.Lines?.push({ ...item, Id });
  }
  addSite(item: SiteNodeConfig, piplineId: string) {
    const Id = item.Id ?? guid();
    const pipline = this.scenario.Lines.find(a => a.Id == piplineId);
    if (pipline) {
      pipline.Sites.push({ ...item, Id });
    }
  }
  addSrm(item: SrmConfig) {
    const Id = item.Id ?? guid();
    this.scenario.Srms?.push({ ...item, Id });
  }

  removePipline(item: PiplineConifg) {
    this.scenario.Lines = this.scenario.Lines?.filter(a => a.Id != item.Id);
  }
  removeSite(item: SiteNodeConfig, piplineId: string) {
    const pipline = this.scenario.Lines.find(a => a.Id == piplineId);
    if (pipline) {
      pipline.Sites = pipline.Sites?.filter(a => a.Id != item.Id);
    }
  }
  removeSrm(item: SrmConfig) {
    this.scenario.Srms = this.scenario.Srms?.filter(a => a.Id != item.Id);
  }

  // addNode(node: Node) {
  //   const item = this.metaService.getCellConfig(node);
  //   const { Type } = item;

  //   switch (Type) {
  //     case CellType.Pipline:
  //       {
  //         this.addPipline(item as PiplineConifg);
  //       }
  //       break;
  //     case CellType.Site:
  //       {
  //         const id = node.parent?.id!;
  //         this.addSite(item as SiteNodeConfig, id);
  //       }
  //       break;
  //     case CellType.Srm:
  //       {
  //         this.addSrm(item as SrmConfig);
  //       }
  //       break;
  //     default:
  //       break;
  //   }
  // }

  getScenario() {
    return this.scenario;
  }
}
